#include "Chest.h"

#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_DirectInput.h"

#include "Game.h"
#include "GamePlayState.h"
#include "MinigameState.h"
#include "CreatePickUpMessage.h"
#include "PickUp.h"
#include "MessageSystem.h"
#include "Player.h"

int Chest::ImageID = -1;
int Chest::Count = 0;

int Chest::buttonA = -1;


// Constructor
Chest::Chest(void) 
{
	// If this is the first chest, load the image
	if( ImageID == -1 )
		ImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("graphics/chest.png"),D3DCOLOR_XRGB(0,255,0));

	// Set the image id
	SetTextureID(ImageID);

	// Increase the count
	++Count;

	// Set the chest to be closed
	CurrentState = CLOSED;

	// Set it to be a chest
	type = OBJ_CHEST;

	// Set the collision box, I guess
	SetWidth(32);
	SetHeight(32);

	// There's no player nearby when it spawns
	PlayerNear = false;
	InGame = false;

	if( buttonA == -1 )
		buttonA = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/ButtonA.png"));

}

// Destructor
Chest::~Chest(void) 
{
	// Decrease the cout
	--Count;

	// If there's no more chests, unload the image
	if( Count == 0 && ImageID != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(ImageID);
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonA);
		ImageID = -1;
	}
	if( Count == 0 && buttonA != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonA);
		buttonA = -1;
	}
}

// Update
void Chest::Update(float elapsed)
{
	// Get the camera
	int camx = GamePlayState::GetInstance()->GetCamX();
	int camy = GamePlayState::GetInstance()->GetCamY();

	// Don't update if it's off screen
	if( GetX() < camx || GetY() < camy || GetX() > camx + 800 || GetY() > camy + 600)
		return;

	if( PlayerNear && CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0, playerNum) && InGame == false)
	{
		GamePlayState::GetInstance()->inMinigame = true;
		MinigameState::GetInstance()->StartGame(playerNum);
		InGame = true;
	}

	// Reset whether a player is near or not
	PlayerNear = false;

	if( InGame )
	{
		// Failed
		if( MinigameState::GetInstance()->Failed )
		{
			CurrentState = BROKE;
			InGame = false;
		}
		// Victory
		if( MinigameState::GetInstance()->Won )
		{
			CurrentState = OPEN;
			// Create some pickups
			CreatePickUpMessage* loot = new CreatePickUpMessage(METAL,rand()%10 + 10,GetVectorPos(), GetType());
			MessageSystem::GetInstance()->SendMsg(loot);
			loot = new CreatePickUpMessage(CIRCUITRY,rand()%10 + 10,GetVectorPos(), GetType());
			MessageSystem::GetInstance()->SendMsg(loot);
			loot = NULL;
			// Increase the health/energy of the player who won the game
			Player* winner = nullptr;
			winner = ((playerNum) ? GamePlayState::GetInstance()->GetPlayer2() : GamePlayState::GetInstance()->GetPlayer() );
			if( winner )
			{
				winner->AddRef();
				winner->TakeDam( -50.0f );
				winner->SetEnergy(winner->GetEnergy() + 15);
				winner->Release();
			}
			InGame = false;
		}
	}
}

// Draw
void Chest::Render()
{
	// Get the camera
	int camx = GamePlayState::GetInstance()->GetCamX();
	int camy = GamePlayState::GetInstance()->GetCamY();

	// Don't draw if it's off screen
	if( GetX() < camx || GetY() < camy || GetX() > camx + 800 || GetY() > camy + 600)
		return;

	// The area of the picture to render
	RECT render = {0,0,32,32};
	switch ( CurrentState )
	{
	case CLOSED:
		break;
	case OPEN:
		render.left = 32;
		render.right = 64;
		break;
	case BROKE:
		render.top = 32;
		render.bottom = 64;
		break;
	}

	// Draw the chest
	CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)GetY() - GamePlayState::GetInstance()->GetCamY(),1,1,&render);

	// Draw the indicator
	if( PlayerNear )
		CSGD_TextureManager::GetInstance()->Draw(buttonA,(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)(GetY()-32) - GamePlayState::GetInstance()->GetCamY(),.5f,.5f);

		//Game::GetInstance()->DrawBitmapFont("A",(int)GetX() + 6 - GamePlayState::GetInstance()->GetCamX(),(int)GetY() - 32 - GamePlayState::GetInstance()->GetCamY(),1,D3DCOLOR_XRGB(0,150,200));
}

// Check collision
bool Chest::CheckCollision(IEntity* base)
{
	if( Entity::CheckCollision(base) && base->GetType() == OBJ_PLAYER && CurrentState == CLOSED)
	{
		Player* p = dynamic_cast<Player*>(base);
		if( p )
		{
			p->AddRef();
			if(p->GetPlayerNum() == 1)
				playerNum = 0;
			else
				playerNum = 1;
			PlayerNear = true;
			p->CanActivate(true);
			p->Release();
			return true;
		}
	}
	return false;
}

// Get the current state
unsigned char Chest::GetState(void)
{
	return CurrentState;
}